package com.deer.wms.base.system.web;

import com.deer.wms.base.system.model.Pack.*;
import com.deer.wms.base.system.service.PackDetailService;
import com.deer.wms.base.system.service.PackMasterService;
import com.deer.wms.intercept.annotation.User;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.util.DateUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author guo
 * @since 2019/11/18
 */
@Api(description = "包装master信息api接口")
@RestController
@RequestMapping("/pack/masters")
public class PackMasterController {

    @Autowired
    private PackMasterService packMasterService;
    @Autowired
    private PackDetailService packDetailService;


    @ApiOperation(value = "新增packMaster以及packMaster列表", notes = "新增packMaster以及packMaster列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class, required = true)
    })
    @PostMapping("/insert")
    public Result add(@RequestBody InsertData insertData, @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        PackMasterCriteria criteria = new PackMasterCriteria();
        String packCode = insertData.getPackMaster().getPackCode();
        criteria.setPackCode(packCode);
        //先判断包装是否已经存在
        List<PackMasterDto> list = packMasterService.findList(criteria);
        if (list.size() == 0) {
            //新增PackMaster主表数据
            PackMaster packMaster = insertData.getPackMaster();
            String nowDate = DateUtils.getNowDateTimeString();
            //String packCode ="BZ" + "-"+ RandomNo.createNo();
            packMaster.setCreateTime(nowDate);
            packMasterService.save(packMaster);

            //新增子表数据
            List<PackDetail> packDetails = insertData.getPackDetails();
            for (PackDetail packDetail : packDetails) {
                packDetail.setPackCode(packCode);
            }
            packDetailService.saveBatch(packDetails);
        } else {
            return ResultGenerator.genSuccessResult(CommonCode.PACK_CODE_EXISTS, null);
        }
        return ResultGenerator.genSuccessResult();
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class, required = true)
    })
    @Transactional
    @GetMapping("/delete")
    @ApiOperation(value = "删除packMaster信息", notes = "删除packMaster信息")
    public Result delete(String packCode, @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        packMasterService.deleteByCode(packCode);
        packDetailService.deleteByCode(packCode);
        return ResultGenerator.genSuccessResult();
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class, required = true)
    })
    @Transactional
    @PostMapping("/update")
    @ApiOperation(value = "更新packMaster信息", notes = "更新packMaster信息")
    public Result update(@RequestBody InsertData insertData, @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        List<PackMasterDto> list = packMasterService.findByPackCode(insertData.getPackMaster().getPackCode());
        if (list.size() == 0 || list.size() == 1 && list.get(0).getPackCode().equals(insertData.getPackMaster().getPackCode())) {
            PackMaster packMaster = insertData.getPackMaster();
            packMasterService.updateById(packMaster);
            List<PackDetail> packDetails = insertData.getPackDetails();
            for (PackDetail packDetail : packDetails) {
                packDetailService.updateById(packDetail);
            }
        } else {
            return ResultGenerator.genSuccessResult(CommonCode.PACK_CODE_EXISTS, null);
        }
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/detail")
    public Result detail(Integer id) {
        PackMaster packMaster = packMasterService.getById(id);
        return ResultGenerator.genSuccessResult(packMaster);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class, required = true)
    })
    @Transactional
    @GetMapping("/list")
    public Result list(PackMasterCriteria criteria, @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<PackMasterDto> list = packMasterService.findList(criteria);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

}
